/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"


/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay( void )
{
	unsigned int i;
	
	for( i = 0 ; i < 2000 ; i++ ){}
}


/**
 * @brief       EINT3ISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void EINT3ISR (void) interrupt ISR_EINT3
{
	unsigned char int_stat = INTC->eint_stat;
	
	if ((int_stat & 0x08) == 0x08)
		INTC->eint_stat = 0x08;
	
		PD1=~PD1;
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
}


/**
 * @brief       EINT2ISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void EINT2ISR (void) interrupt ISR_EINT2
{
	unsigned char int_stat = INTC->eint_stat;
	
	if ((int_stat & 0x04) == 0x04)
		INTC->eint_stat = 0x04;
	
	PD1=~PD1;
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
}


/**
 * @brief       EINT1ISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void EINT1ISR (void) interrupt ISR_EINT1
{
	unsigned char int_stat = INTC->eint_stat;
	
	if ((int_stat & 0x02) == 0x02)
		INTC->eint_stat = 0x02;
	
	PD1=~PD1;
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
}


/**
 * @brief       EINT0ISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void EINT0ISR (void) interrupt ISR_EINT0
{
	unsigned char int_stat = INTC->eint_stat;
	
	if ((int_stat & 0x01) == 0x01)
		INTC->eint_stat = 0x01;
	
	PD1=~PD1;
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
}


/**
 * @brief       EINT3Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void EINT3Init(void)
{
	/* enable PA.2 input */
	PORT_SetPAInput(BIT2);
	
	/* enable PA.2 EINT function */
	Set_PA2_EINT3();
	
	/* enable PA.2 falling & low interrupt_ */
	//Set_EINT3_Falling_and_Low();
	
	/* enable PA.2 rising interrupt_ */
	//Set_EINT3_Rising();
	
	/* enable PA.2 falling interrupt_*/
	Set_EINT3_Falling();
	
	/* enable PA.2 rising & falling interrupt_ */
	//Set_EINT3_Rising_and_Falling();
	
	/* enable PA.2 interrupt_ */
	Enable_EINT3_interrupt();
	
}


/**
 * @brief       EINT2Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void EINT2Init(void)
{
	/* enable PA.3 input */
	PORT_SetPAInput(BIT3);
	
	/* enable PA.3 EINT function */
	Set_PA3_EINT2();
	
	/* enable PA.3 falling & low interrupt_ */
	//Set_EINT2_Falling_and_Low();
	
	/* enable PA.3 rising interrupt_ */
	//Set_EINT2_Rising();
	
	/* enable PA.3 falling interrupt_ */
	Set_EINT2_Falling();
	
	/* enable PA.3 rising & falling interrupt_ */
	//Set_EINT2_Rising_and_Falling();
	
	/* enable PA.3 interrupt_ */
	Enable_EINT2_interrupt();
}


/**
 * @brief       EINT1Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void EINT1Init(void)
{
	/* enable PA.5 input */
	PORT_SetPAInput(BIT5);
	
	/* enable PA.5 EINT function */
	Set_PA5_EINT1();
	
	/* enable PA.5 falling & low interrupt_ */
	//Set_EINT1_Falling_and_Low();
	
	/* enable PA.5 rising interrupt_ */
	//Set_EINT1_Rising();
	
	/* enable PA.5 falling interrupt_ */
	Set_EINT1_Falling();
	
	/* enable PA.5 rising & falling interrupt_ */
	//Set_EINT1_Rising_and_Falling();
	
	/* enable PA.5 interrupt */
	Enable_EINT1_interrupt();
	
}


/**
 * @brief       EINT0Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void EINT0Init(void)
{
	/* enable PB.3 input */
	PORT_SetPBInput(BIT3);
	
	/* enable PB.3 EINT function */
	Set_PB3_EINT0();
	
	/* enable PB.3 falling & low interrupt_ */
	//Set_EINT0_Falling_and_Low();
	
	/* enable PB.3 rising interrupt_ */
	//Set_EINT0_Rising();
	
	/* enable PB.3 falling interrupt_ */
	Set_EINT0_Falling();
	
	/* enable PB.3 rising & falling interrupt_ */
//	Set_EINT0_Rising_and_Falling();
	
	/* enable PB.3 interrupt_ */
	Enable_EINT0_interrupt();
}


/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main( void )
{
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable();												// enable GPIOA GPIOB GPIOC GPIOD clock
	
	/* Set PC7 input as button */
	PORT_SetPCInput(BIT7);

	/* Set PD1. ,PD2.,PD3 output */
	PORT_SetPDPushPull(BIT1 | BIT2 | BIT3 );


	PD1 = 0; // LED_G1 On
	PD2 = 1 ; //LED_R Off
	PD3= 0;  // LED_G2  On
	
	/* enable external interrupt_ sampling clock */
	//Set_EINT_ClockSource_LSRC();																// external interrupt_ sampling source clock = lsrc 
	
	/*enable external interrupt_ sampling source clock*/
	En_EINT_clk();																													
	
	/* enable EINT0 function */
	EINT0Init();
	
	/* enable EINT1 function */
	//EINT1Init();
	
	/* enable EINT2 function */
	//EINT2Init();
	
	/* enable EINT3 function */
	//EINT3Init();
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
	
	while(1)
	{
		if( Get_PCx_input(7) == 0)   // PC7  = 0 , button down
			{
					PD2 = 0 ; //LED_R On
			}
			else
			{
					PD2 = 1 ; //LED_R Off
			}
	}
}

